package com.zhengdongzulin.zuke.app.main.mine.collection

import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.flowlayout.DefaultFlowTagAdapter
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.CollectionHouseType
import com.zhengdongzulin.zuke.app.data.FindHouse
import com.zhengdongzulin.zuke.app.main.apartment.ZKHouseDetailsActivity
import com.zhengdongzulin.zuke.app.main.apartment.sign.ZKSign1Activity
import com.zhengdongzulin.zuke.app.main.findhouse.ZKRoomDetailsActivity
import com.zhengdongzulin.zuke.app.main.mine.ZKIdAuthActivity
import com.zhengdongzulin.zuke.base.BaseFragment
import com.zhengdongzulin.zuke.databinding.ZkFragmentCollectionFootprintBinding
import com.zhengdongzulin.zuke.ext.*


/**
 * jeff
 * 实名认证
 * 2021-03-27 17:50:47
 */
class ZKCollectionFootprintFragment(var mType: CollectionFootprintType) :
    BaseFragment<CollectionFootprintVm, ZkFragmentCollectionFootprintBinding>() {

    var mSignId = ""

    enum class CollectionFootprintType {
        COLLECTION_ROOM,//收藏房间
        COLLECTION_HOUSE_TYPE,//收藏房型
        FOOTPRINTN_ROOM,//房间足迹
        FOOTPRINT_HOUSE_TYPE,//房型足迹
        RESERVE_ROOM,//预约房间
        RESERVE_HOUSE_TYPE,//预约房型
        RESERVE_2_ROOM,//预定房间
        RESERVE_2_HOUSE_TYPE//预定房型
    }

    companion object {
        fun newInstance(type: CollectionFootprintType) = ZKCollectionFootprintFragment(type)
    }

    override fun getLayoutResId(): Int = R.layout.zk_fragment_collection_footprint

    override fun initVM(): CollectionFootprintVm = getVM(CollectionFootprintVm::class.java)

    override fun initView() {
        mView.contentView.setEnableRefresh(false)
        mView.statusView.showLoading()
    }

    override fun initData() {
    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    fun getData() {
        when (mType) {
            CollectionFootprintType.COLLECTION_ROOM -> {
                mViewModel.collection_list()
            }
            CollectionFootprintType.COLLECTION_HOUSE_TYPE -> {
                mViewModel.collection_list_room_type()
            }
            CollectionFootprintType.FOOTPRINTN_ROOM -> {
                mViewModel.access_list()
            }
            CollectionFootprintType.FOOTPRINT_HOUSE_TYPE -> {
                mViewModel.access_list_room_type()
            }
            CollectionFootprintType.RESERVE_ROOM -> {
                mViewModel.reservation_list()
            }
            CollectionFootprintType.RESERVE_HOUSE_TYPE -> {
                mViewModel.reservation_list_room_type()
            }
            CollectionFootprintType.RESERVE_2_ROOM -> {
                mViewModel.book_list()
            }
            CollectionFootprintType.RESERVE_2_HOUSE_TYPE -> {
                mViewModel.book_list_room_type()
            }
        }
    }

    override fun initListener() {

    }

    override fun startObserve() {
        mViewModel.collection_list.observe(this, androidx.lifecycle.Observer {
            if (it.size == 0) {
                mView.statusView.showEmpty()
                return@Observer
            }
            mView.recyclerView.vertical().bindData(
                it,
                R.layout.item_find_house
            ) { holder, item, position ->
                holder.getView<ImageView>(R.id.photo)
                    .loadImage(item.picUrl, R.mipmap.error_empty_zheng, 5f)
                holder.setText(R.id.title_name, item.houseName)
                holder.setText(
                    R.id.desc,
                    "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
                )
                holder.setText(R.id.address, item.address)
                var flow = holder.getView<FlowTagLayout>(R.id.flow)
                flow.setAdapter(object : DefaultFlowTagAdapter(requireActivity()) {
                    override fun getLayoutId(): Int {
                        return R.layout.item_find_house_list_flow
                    }
                })
                flow.setItems(item.featureList)
                var money = holder.getView<TextView>(R.id.money)
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    money.text = ""
                    money.appendStyleSpan("面议")
                } else {
                    money.text = "￥${item.rentPrice}"
                    money.appendSizeSpan("/月", 0.46f)
                        .colorSpan(
                            "",
                            money.text.length - 2..money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..money.text.length - 2)
                }
                var state = holder.getView<TextView>(R.id.state)
                state.text = item.statusName
                state.visible()
                if (item.status == "20") {
                    state.setColor(R.color.color_1273f7)
                    state.setBackgroundResource(R.drawable.collection_footprint_state_bg_yellow)
                } else {
                    state.setColor(R.color.gray_8fa4bc)
                    state.setBackgroundResource(R.drawable.collection_footprint_state_bg_gray)
                }
            }.itemClick { data: List<FindHouse>, holder, position ->
                Intent().put("id", data[position].houseId)
                    .start(requireActivity(), ZKRoomDetailsActivity::class.java)
            }
            mView.statusView.showContent()
        })
        mViewModel.collection_list.state.observe(
            this,
            getObserver(mViewModel.collection_list)
        )

        mViewModel.collection_list_room_type.observe(this, androidx.lifecycle.Observer {
            if (it.size == 0) {
                mView.statusView.showEmpty()
                return@Observer
            }
            mView.recyclerView.vertical().bindData(
                it,
                R.layout.item_apartment_details_list
            ) { holder, item, position ->
                holder.getView<ImageView>(R.id.img)
                    .loadImage(item.coverPic, R.mipmap.error_empty_zheng, 5f)
                holder.setText(R.id.name, item.name)
                holder.setText(
                    R.id.desc,
                    "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
                )
                var money = holder.getView<TextView>(R.id.money)
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    money.text = ""
                    money.appendStyleSpan("面议")
                } else {
                    money.text = "￥${item.rentPrice}"
                    money.appendSizeSpan("/月", 0.46f)
                        .colorSpan(
                            "",
                            money.text.length - 2..money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..money.text.length - 2)
                }
            }.itemClick { data: List<CollectionHouseType>, holder, position ->
                Intent().put("id", data[position].roomTypeId)
                    .put("houseItemName", data[position].houseItemName).start(
                        requireActivity(),
                        ZKHouseDetailsActivity::class.java
                    )
            }
            mView.statusView.showContent()
        })
        mViewModel.collection_list_room_type.state.observe(
            this,
            getObserver(mViewModel.collection_list_room_type)
        )

        mViewModel.reservation_list.observe(this, androidx.lifecycle.Observer {
            if (it.size == 0) {
                mView.statusView.showEmpty()
                return@Observer
            }
            mView.recyclerView.vertical().bindData(
                it,
                R.layout.item_reserve
            ) { holder, item, position ->
                holder.getView<View>(R.id.name).gone()
                val bt2 = holder.getView<TextView>(R.id.bt2)
                val bt1 = holder.getView<View>(R.id.bt1)
                if (item.status == "1") {//待受理
                    holder.getView<View>(R.id.v_bg)
                        .setBackgroundColor(Color.parseColor("#3D1273f7"))
                    holder.setColor(R.id.title_name, R.color.color_1273f7)
                    holder.setColor(R.id.status, R.color.color_1273f7)
                    holder.setColor(R.id.time, R.color.color_1273f7)
                    holder.setColor(R.id.money, R.color.color_1273f7)
                    holder.setColor(R.id.bt1, R.color.color_1273f7)
                    bt1.setBackgroundResource(R.drawable.item_reserve_button)
                    bt2.visible()
                } else {//已受理，已关闭
                    holder.getView<View>(R.id.v_bg).setBackgroundColor(color(R.color.gray_f0))
                    holder.setColor(R.id.title_name, R.color.black_66)
                    holder.setColor(R.id.status, R.color.black_66)
                    holder.setColor(R.id.time, R.color.black_66)
                    holder.setColor(R.id.money, R.color.black_66)
                    holder.setColor(R.id.bt1, R.color.black_66)
                    bt1.setBackgroundResource(R.drawable.item_reserve_button_gray)
                    bt2.gone()
                }
                when (mType) {
                    CollectionFootprintType.RESERVE_ROOM -> {
                        holder.setColor(R.id.bt2, R.color.black_66)
                        bt2.setBackgroundResource(R.drawable.item_reserve_button_gray)
                        bt2.setText("取消预约")
                        bt2.setOnClickListener {
                            MaterialDialog.Builder(requireContext())
                                .content("是否取消预约？")
                                .negativeText("取消")
                                .positiveText("确定")
                                .positiveColor(color(R.color.color_1273f7))
                                .contentColor(color(R.color.black_66))
                                .negativeColor(color(R.color.gray_99))
                                .cancelable(false)
                                .onNegative { dialog, which ->
                                    dialog.dismiss()
                                }
                                .onPositive { dialog, which ->
                                    mViewModel.reservation_cancel(item.bizId)
                                }
                                .show()
                        }
                    }
                    CollectionFootprintType.RESERVE_2_ROOM -> {
                        holder.setColor(R.id.bt2, R.color.color_1273f7)
                        bt2.setBackgroundResource(R.drawable.item_reserve_button)
                        bt2.setText("立即签约")
                        bt2.setOnClickListener {
                            mSignId = item.houseId
                            mViewModel.check_sign_online("houseId", item.houseId)
                        }
                    }
                }
                bt1.setOnClickListener {
                    requireActivity().callPhoneDialog(
                        item.managerInfo.managerName,
                        item.managerInfo.managerPhone
                    ).show()
                }
                holder.getView<ImageView>(R.id.img)
                    .loadImage(item.picUrl, R.mipmap.error_empty_zheng, 5f)
                holder.setText(R.id.title_name, item.houseName)
                holder.setText(R.id.status, item.statusName)
                holder.setText(R.id.time, "预约看房时间：${item.createTime}")
                var money = holder.getView<TextView>(R.id.money)
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    money.text = ""
                    money.appendStyleSpan("面议")
                } else {
                    money.text = "￥${item.rentPrice}"
                    money.appendSizeSpan("/月", 0.46f)
                        .styleSpan("", 0..money.text.length - 2)
                    holder.setText(
                        R.id.desc,
                        "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
                    )
                }
                holder.setText(R.id.address, item.address)
                var flow = holder.getView<FlowTagLayout>(R.id.flow)
                flow.setAdapter(object : DefaultFlowTagAdapter(requireActivity()) {
                    override fun getLayoutId(): Int {
                        return R.layout.item_find_house_list_flow
                    }
                })
                flow.setItems(item.featureList)
            }.itemClick { data: List<FindHouse>, holder, position ->
                Intent().put("id", data[position].houseId)
                    .start(requireActivity(), ZKRoomDetailsActivity::class.java)
            }
            mView.statusView.showContent()
        })
        mViewModel.reservation_list.state.observe(
            this,
            getObserver(mViewModel.reservation_list)
        )

        mViewModel.reservation_list_room_type.observe(this, androidx.lifecycle.Observer {
            if (it.size == 0) {
                mView.statusView.showEmpty()
                return@Observer
            }
            mView.recyclerView.vertical().bindData(
                it,
                R.layout.item_reserve
            ) { holder, item, position ->
                holder.getView<View>(R.id.flow).gone()
                val bt2 = holder.getView<TextView>(R.id.bt2)
                val bt1 = holder.getView<View>(R.id.bt1)
                if (item.status == "1") {//待受理
                    holder.getView<View>(R.id.v_bg)
                        .setBackgroundColor(color(R.color.color_3D1273f7))
                    holder.setColor(R.id.title_name, R.color.color_1273f7)
                    holder.setColor(R.id.status, R.color.color_1273f7)
                    holder.setColor(R.id.time, R.color.color_1273f7)
                    holder.setColor(R.id.money, R.color.color_1273f7)
                    holder.setColor(R.id.bt1, R.color.color_1273f7)
                    bt1.setBackgroundResource(R.drawable.item_reserve_button)
                    bt2.visible()
                } else {//已受理，已关闭
                    holder.getView<View>(R.id.v_bg).setBackgroundColor(color(R.color.gray_f0))
                    holder.setColor(R.id.title_name, R.color.black_66)
                    holder.setColor(R.id.status, R.color.black_66)
                    holder.setColor(R.id.time, R.color.black_66)
                    holder.setColor(R.id.money, R.color.black_66)
                    holder.setColor(R.id.bt1, R.color.black_66)
                    bt1.setBackgroundResource(R.drawable.item_reserve_button_gray)
                    bt2.gone()
                }
                when (mType) {
                    CollectionFootprintType.RESERVE_HOUSE_TYPE -> {
                        holder.setColor(R.id.bt2, R.color.black_66)
                        bt2.setBackgroundResource(R.drawable.item_reserve_button_gray)
                        bt2.setText("取消预约")
                        bt2.setOnClickListener {
                            MaterialDialog.Builder(requireContext())
                                .content("是否取消预约？")
                                .negativeText("取消")
                                .positiveText("确定")
                                .positiveColor(color(R.color.color_1273f7))
                                .contentColor(color(R.color.black_66))
                                .negativeColor(color(R.color.gray_99))
                                .cancelable(false)
                                .onNegative { dialog, which ->
                                    dialog.dismiss()
                                }
                                .onPositive { dialog, which ->
                                    mViewModel.reservation_cancel(item.bizId)
                                }
                                .show()
                        }
                    }
                    CollectionFootprintType.RESERVE_2_HOUSE_TYPE -> {
                        holder.setColor(R.id.bt2, R.color.color_1273f7)
                        bt2.setBackgroundResource(R.drawable.item_reserve_button)
                        bt2.setText("立即签约")
                        bt2.setOnClickListener {
                            mSignId = item.houseId
                            mViewModel.check_sign_online("houseId", item.houseId)
                        }
                    }
                }
                bt1.setOnClickListener {
                    requireActivity().callPhoneDialog(
                        item.managerInfo.managerName,
                        item.managerInfo.managerPhone
                    ).show()
                }
                holder.getView<ImageView>(R.id.img)
                    .loadImage(item.coverPic, R.mipmap.error_empty_zheng, 5f)
                holder.setText(R.id.title_name, item.houseItemName)
                holder.setText(
                    R.id.name,
                    "${item.name}"
                )
                holder.setText(R.id.status, item.statusName)
                holder.setText(R.id.time, "预约看房时间：${item.createTime}")
                var money = holder.getView<TextView>(R.id.money)
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    money.text = ""
                    money.appendStyleSpan("面议")
                } else {
                    money.text = "￥${item.rentPrice}"
                    money.appendSizeSpan("/月", 0.46f)
                        .styleSpan("", 0..money.text.length - 2)
                    holder.setText(
                        R.id.desc,
                        "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
                    )
                }
                holder.setText(R.id.address, item.address)
            }.itemClick { data: List<CollectionHouseType>, holder, position ->
                Intent().put("id", data[position].roomTypeId)
                    .put("houseItemName", data[position].houseItemName)
                    .start(requireActivity(), ZKHouseDetailsActivity::class.java)
            }
            mView.statusView.showContent()
        })
        mViewModel.reservation_list_room_type.state.observe(
            this,
            getObserver(mViewModel.reservation_list_room_type)
        )
        mViewModel.reservation_cancel.observe(this, Observer {
            toast("取消成功")
            getData()
        })
        mViewModel.reservation_cancel.state.observe(
            this,
            getObserver(mViewModel.reservation_cancel)
        )
        mViewModel.check_sign_online.observe(this, Observer {
            if (!it.isNullOrEmpty()) {
                when (it) {//0成功，跳转到在线签约页面，1未实名认证，跳转到实名认证 2无电子模版
                    "0" -> {
//                        Intent().put("houseId", mSignId)
//                            .start(requireActivity(), ZKReserveSearchActivity::class.java)
                        //屏蔽在线签约
//                        Intent().put("houseId", mSignId)
//                            .start(requireActivity(), ZKSign1Activity::class.java)


                        MaterialDialog.Builder(requireContext())
                            .content("当前房源不支持在线签约！")
                            .positiveText("确定")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .cancelable(false)
                            .onPositive { dialog, which ->
                                dialog.dismiss()
                            }
                            .show()

                    }
                    "1" -> {
                        MaterialDialog.Builder(requireContext())
                            .content("未实名认证，请您先进行实名认证")
                            .negativeText("取消")
                            .positiveText("去实名")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .negativeColor(color(R.color.gray_99))
                            .cancelable(false)
                            .onNegative { dialog, which ->
                                dialog.dismiss()
                            }
                            .onPositive { dialog, which ->
                                Intent().start(requireActivity(), ZKIdAuthActivity::class.java)
                                dialog.dismiss()
                            }
                            .show()
                    }
                    "2" -> {
                        MaterialDialog.Builder(requireContext())
                            .content("当前房型未配置电子合同模板\n暂不支持签约！")
                            .positiveText("确定")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .cancelable(false)
                            .onPositive { dialog, which ->
                                dialog.dismiss()
                            }
                            .show()
                    }
                    else -> {
                        toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
                    }
                }
            } else {
                toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
            }
        })
        mViewModel.check_sign_online.state.observe(this, getObserver(mViewModel.check_sign_online))
    }

}